package com.dong.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.dong.dao.UserDao;
import com.dong.entity.UserEntity;

public class UserDaoImpl implements UserDao {

	@Override
	public void add(UserEntity userName) {
		Connection conn = JDBC.getConn();
		String sql = "insert into t_ajax(user_name,pass_word)value(?,?)";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, userName.getUserName());
			ps.setString(2, userName.getPassword());
//			ps.setString(3, userName.getDeleteLogo() == null ? "N" : userName.getDeleteLogo());
			ps.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JDBC.close(null, ps, conn);
	}

	@Override
	public void delete(Long id) {
		Connection conn = JDBC.getConn();
		String sql = "update t_ajax set delete_logo=? where id=? ";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, "Y");
			ps.setLong(2, id);
			ps.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JDBC.close(null, ps, conn);
	}

	@Override
	public void update(UserEntity userName) {
		Connection conn = JDBC.getConn();
		String sql = "update t_ajax set user_name=?, pass_word=? where id=?";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, userName.getUserName());
			ps.setString(2, userName.getPassword());
			ps.setLong(3, userName.getId());
			ps.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JDBC.close(null, ps, conn);

	}

	@Override
	public List<UserEntity> listAll() {
		Connection conn = JDBC.getConn();
		String sql = "select id,user_name,pass_word,creation_time,delete_logo from t_ajax where delete_logo='N'";
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<UserEntity> list = new ArrayList();
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				int i = 1;
				Long id = rs.getLong(i++);
				String userName = rs.getString(i++);
				String password = rs.getString(i++);
				Date creationTime = rs.getTimestamp(i++);
				String deleteLogo = rs.getString(i++);

				list.add(new UserEntity(id, userName,password, creationTime, deleteLogo));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JDBC.close(null, ps, conn);
		return list;
	}

	@Override
	public UserEntity getById(Long id) {
		Connection conn = JDBC.getConn();
		String sql = "select id,user_name,pass_word,creation_time,delete_logo from t_ajax where id=?";
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setLong(1, id);
			rs = ps.executeQuery();
			if (rs.next()) {
				int i = 1;
				UserEntity user = new UserEntity();
				user.setId(rs.getLong(i++));
				user.setUserName(rs.getString(i++));
				user.setPassword(rs.getString(i++));
				user.setCreationTime(rs.getDate(i++));
				user.setDeleteLogo(rs.getString(i++));
				return user;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JDBC.close(null, ps, conn);

		return null;
	}

}
